fix: file extension is not added to imported files when using NodeNext moduleResolution - #3361
Conversation
…t moduleResolution
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThreads tsconfig-driven import-extension logic through codegen: adds getImportExtension, extends Tsconfig types, passes computed importExtension into generateImports and schema writers, updates orchestration, and adds tests covering NodeNext and allowImportingTsExtensions behaviors. ChangesImport extension computation and threading
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/core/src/writers/schemas.ts (1)
225-227: 💤 Low valueConsider adding a clarifying comment for the fallback branch.
The fallback
.replace(/\.ts$/, '')handles edge cases whererelativedoesn't end withfileExtension. Sincecanonical.importPathis constructed viagetPathwithfileExtension, the primary branch should handle most cases. A brief comment explaining when the fallback is needed would improve code clarity.Example:
const withoutFileExtension = relative.endsWith(fileExtension) ? relative.slice(0, -fileExtension.length) : relative.replace(/\.ts$/, ''); // Fallback for legacy paths with hardcoded .ts extension🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/writers/schemas.ts` around lines 225 - 227, Add a short clarifying comment on the fallback branch for the withoutFileExtension computation: explain that relative.endsWith(fileExtension) handles normal paths produced by getPath (used to build canonical.importPath) and the else branch (relative.replace(/\.ts$/, '')) is a defensive fallback for legacy or hardcoded ".ts" paths that don't match the current fileExtension; place the comment next to the else branch referencing withoutFileExtension, relative, fileExtension, and canonical.importPath for context.packages/core/src/utils/tsconfig.ts (1)
32-34: ⚡ Quick winIncomplete extension mapping for NodeNext/Node16 module resolution.
The current implementation only converts
.ts→.jsfor NodeNext/Node16 module resolution. According to TypeScript's module resolution rules, additional extensions should also be mapped:
.tsx→.jsx.mts→.mjs.cts→.cjsWhile Orval likely generates only
.tsfiles currently, adding complete support makes this utility more robust and prevents edge-case bugs if other extensions are introduced later.♻️ Proposed fix to handle all TypeScript extensions
if ( (module && NODE_NEXT_MODULES.has(module)) || (moduleResolution && NODE_NEXT_MODULES.has(moduleResolution)) ) { - return fileExtension.endsWith('.ts') - ? `${fileExtension.slice(0, -3)}.js` - : fileExtension; + if (fileExtension.endsWith('.ts')) { + return `${fileExtension.slice(0, -3)}.js`; + } + if (fileExtension.endsWith('.tsx')) { + return `${fileExtension.slice(0, -4)}.jsx`; + } + if (fileExtension.endsWith('.mts')) { + return `${fileExtension.slice(0, -4)}.mjs`; + } + if (fileExtension.endsWith('.cts')) { + return `${fileExtension.slice(0, -4)}.cjs`; + } + return fileExtension; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/utils/tsconfig.ts` around lines 32 - 34, The current return expression only maps `.ts`→`.js`; update the extension mapping logic (where `fileExtension` is used) to handle the NodeNext/Node16 mappings as well: `.ts`→`.js`, `.tsx`→`.jsx`, `.mts`→`.mjs`, and `.cts`→`.cjs` (e.g. replace the simple slice with a small mapping or switch that checks `fileExtension` and returns the corresponding mapped extension, leaving other extensions unchanged).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/core/src/utils/tsconfig.ts`:
- Around line 32-34: The current return expression only maps `.ts`→`.js`; update
the extension mapping logic (where `fileExtension` is used) to handle the
NodeNext/Node16 mappings as well: `.ts`→`.js`, `.tsx`→`.jsx`, `.mts`→`.mjs`, and
`.cts`→`.cjs` (e.g. replace the simple slice with a small mapping or switch that
checks `fileExtension` and returns the corresponding mapped extension, leaving
other extensions unchanged).
In `@packages/core/src/writers/schemas.ts`:
- Around line 225-227: Add a short clarifying comment on the fallback branch for
the withoutFileExtension computation: explain that
relative.endsWith(fileExtension) handles normal paths produced by getPath (used
to build canonical.importPath) and the else branch (relative.replace(/\.ts$/,
'')) is a defensive fallback for legacy or hardcoded ".ts" paths that don't
match the current fileExtension; place the comment next to the else branch
referencing withoutFileExtension, relative, fileExtension, and
canonical.importPath for context.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3be8895c-0871-430d-9f61-0bf05c8e012c
📒 Files selected for processing (7)
packages/core/src/generators/imports.tspackages/core/src/types.tspackages/core/src/utils/tsconfig.tspackages/core/src/writers/generate-imports-for-builder.tspackages/core/src/writers/schemas.test.tspackages/core/src/writers/schemas.tspackages/orval/src/write-specs.ts
…Rabbit suggestions
|
@rkday-pro, thank you so much for this PR. I just realized that there is one place missing for change to take action and this is when writing zod specs. In particular, this line needs the changes to be applied too. Any chance this can be added? I'd assume this could be labeled as a bug. CC: @melloware |
Fixes #2284.
This basically takes the suggestion from that issue and runs with it:
interface Tsconfignow includes the relevant optionsgetImportExtensionno longer just strips .ts - it takes the tsconfig as an argument and adds .js when those options are settsconfighas to be plumbed through to more places, but I think that's unavoidableI've run this in my project, where the
orval-generated files previously failed typechecking witherror TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './datapoint.js'?, and they now pass.Summary by CodeRabbit
New Features
Tests